<?php
$form = $this->form;
$form->prepare();
?>
<div class="content-wrapper">
    <?php echo $this->form()->openTag($form); ?>
    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right">
            <a href="<?php echo $this->url('sales-order-return');?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回销售发货单列表'); ?></a>
            <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('点击退货'); ?></button>
            <?php echo $this->HelpUrl('salesOrderReturn'); ?>
        </h1>
    </section>

    <section class="content">

        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?php echo $this->translate('销售订单信息');?></h3>
                    </div>

                    <div class="box-body">
                        <?php echo $this->formElementErrors($form->get('dberp_csrf'), ['class'=>'error-message']); ?>
                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('发货单号'); ?>：</b><?php echo $this->escapeHtml($this->sendOrderInfo->getSendOrderSn());?>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('销售单号'); ?>：</b><?php echo $this->escapeHtml($this->sendOrderInfo->getOneSalesOrder()->getSalesOrderSn());?>
                                </div>
                            </div>

                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('订单状态'); ?>：</b><?php echo $this->salesHelper()->salesOrderState($this->sendOrderInfo->getOneSalesOrder()->getSalesOrderState(), 2);?>
                                </div>
                            </div>

                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('付款方式'); ?>：</b><?php echo $this->salesHelper()->orderReceivables($this->sendOrderInfo->getOneSalesOrder()->getReceivablesCode());?>
                                </div>
                            </div>

                        </div>

                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('客户'); ?>：</b><?php echo $this->escapeHtml($this->sendOrderInfo->getOneSalesOrder()->getOneCustomer()->getCustomerName());?>
                                </div>
                            </div>
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('客户联系人'); ?>：</b><?php echo $this->escapeHtml($this->sendOrderInfo->getOneSalesOrder()->getCustomerContacts());?>
                                </div>
                            </div>

                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('客户手机'); ?>：</b><?php echo $this->escapeHtml($this->sendOrderInfo->getOneSalesOrder()->getCustomerPhone());?>
                                </div>
                            </div>

                        </div>

                        <div class="row">
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('客户座机'); ?>：</b><?php echo $this->escapeHtml($this->sendOrderInfo->getOneSalesOrder()->getCustomerTelephone());?>
                                </div>
                            </div>
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('收货地址'); ?>：</b><?php echo $this->escapeHtml($this->sendOrderInfo->getOneSalesOrder()->getCustomerAddress());?>
                                </div>
                            </div>
                            <div class="col-xs-4 form-group">
                                <div class="col-sm-12">
                                    <b><?php echo $this->translate('备注'); ?>：</b><?php echo $this->escapeHtml($this->sendOrderInfo->getOneSalesOrder()->getSalesOrderInfo());?>
                                </div>
                            </div>
                        </div>

                        <div class="box-header">
                            <h4 class="box-title"><?php echo $this->translate('选择退货商品'); ?></h4>
                            &nbsp;
                            <div>
                                <?php echo $this->formElementErrors($form->get('salesGoodsId'), ['class'=>'error-message']); ?>
                                <?php echo $this->formElementErrors($form->get('goodsReturnNum'), ['class'=>'error-message']); ?>
                                <?php echo $this->formElementErrors($form->get('goodsReturnAmount'), ['class'=>'error-message']); ?>
                            </div>
                        </div>
                        <div class="box-body table-responsive no-padding">
                            <table class="table table-hover">
                                <thead>
                                <tr>
                                    <th width="4%"><?php echo $this->translate('选择'); ?></th>
                                    <th width="10%"><?php echo $this->translate('商品编号'); ?></th>
                                    <th width="20%"><?php echo $this->translate('商品名称'); ?></th>
                                    <th width="10%"><?php echo $this->translate('商品规格'); ?></th>
                                    <th width="10%"><?php echo $this->translate('销售单价'); ?></th>
                                    <th width="6%"><?php echo $this->translate('销售数量'); ?></th>
                                    <th width="6%"><?php echo $this->translate('税金'); ?></th>
                                    <th width="10%"><?php echo $this->translate('销售总价'); ?></th>
                                    <th width="8%"><?php echo $this->translate('退货数量'); ?></th>
                                    <th width="12%"><?php echo $this->translate('退货金额'); ?></th>
                                </tr>
                                </thead>

                                <?php
                                foreach ($this->orderGoods as $goodsValue) {
                                    ?>
                                    <tr>
                                        <td><input type="checkbox" title="<?php echo $this->translate('请选择退货商品'); ?>" required minlength="1" name="salesGoodsId[]" value="<?php echo $goodsValue->getSalesGoodsId(); ?>" <?php echo (isset($this->formData['salesGoodsId']) && in_array($goodsValue->getSalesGoodsId(), $this->formData['salesGoodsId'])) ? 'checked' : ''?> class="minimal"></td>
                                        <td><?php echo $this->escapeHtml($goodsValue->getGoodsNumber());?></td>
                                        <td><?php echo $this->escapeHtml($goodsValue->getGoodsName());?></td>
                                        <td><?php echo $this->escapeHtml($goodsValue->getGoodsSpec());?></td>
                                        <td>
                                            <?php echo $this->erpCurrencyFormat($goodsValue->getSalesGoodsPrice());?>
                                            <input type="hidden" name="goods_price_<?php echo $goodsValue->getSalesGoodsId(); ?>" id="goods_price_<?php echo $goodsValue->getSalesGoodsId(); ?>" value="<?php echo $this->erpCurrencyFormat($goodsValue->getSalesGoodsPrice(), '');?>">
                                        </td>
                                        <td><?php echo $this->escapeHtml($goodsValue->getSalesGoodsSellNum().$goodsValue->getGoodsUnit());?></td>
                                        <td><?php echo $this->erpCurrencyFormat($goodsValue->getSalesGoodsTax());?></td>
                                        <td><?php echo $this->erpCurrencyFormat($goodsValue->getSalesGoodsAmount());?></td>
                                        <td><input type="text" min="0.1" max="<?php echo $this->escapeHtml($goodsValue->getSalesGoodsSellNum());?>" oninput="returnGoodsPrice(<?php echo $goodsValue->getSalesGoodsId(); ?>);" name="goodsReturnNum[<?php echo $goodsValue->getSalesGoodsId(); ?>]" id="goods_return_num_<?php echo $goodsValue->getSalesGoodsId(); ?>" value="<?php echo isset($this->formData['goodsReturnNum'][$goodsValue->getSalesGoodsId()]) ? $this->escapeHtml($this->formData['goodsReturnNum'][$goodsValue->getSalesGoodsId()]) : '0'?>" class="form-control"></td>
                                        <td><input type="text" oninput="returnGoodsPrice(<?php echo $goodsValue->getSalesGoodsId(); ?>);" name="goodsReturnAmount[<?php echo $goodsValue->getSalesGoodsId(); ?>]" id="goods_return_amount_<?php echo $goodsValue->getSalesGoodsId(); ?>" value="<?php echo isset($this->formData['goodsReturnAmount'][$goodsValue->getSalesGoodsId()]) ? $this->escapeHtml($this->formData['goodsReturnAmount'][$goodsValue->getSalesGoodsId()]) : '0'?>" class="form-control"></td>
                                    </tr>
                                <?php } ?>


                            </table>
                        </div>
                        <div class="row">
                            <div class="col-xs-12 form-group">
                                <div class="col-sm-12">
                                    <?php echo $this->translate('退货原因'); ?>
                                    <?php echo $this->formElement($form->get('salesOrderReturnInfo')); ?>
                                    <?php echo $this->formElementErrors($form->get('salesOrderReturnInfo'), ['class'=>'error-message']); ?>
                                </div>
                            </div>
                        </div>

                    </div>
                    <div class="box-footer">
                        <?php echo $this->formElement($form->get('dberp_csrf')); ?>
                        <button type="submit" class="btn btn-primary col-md-offset-5"><i class="fa fa-check"></i> <?php echo $this->translate('点击退货'); ?></button>
                    </div>
                </div>


            </div>
        </div>

    </section>
    <?php echo $this->form()->closeTag(); ?>
</div>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<?php echo $form->getName(); ?>").validate();
    });

    function returnGoodsPrice(id) {
        var goodsPrice  = $('#goods_price_'+id).val();
        goodsPrice  = goodsPrice == '' ? 0 : parseFloat(goodsPrice);
        var goodsNum    = $('#goods_return_num_'+id).val();
        goodsNum    = goodsNum == '' ? 1 : parseFloat(goodsNum);

        var goodsAmount = goodsPrice * goodsNum;

        $('#goods_return_amount_'+id).val(erpNumber(goodsAmount, 2));

        sumAmount();
    }
    function sumAmount() {
    }
</script>